package hk.m4s.lr.repair.test.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.GoodsCarModel;
import hk.m4s.lr.repair.test.ui.goods.GoodsCarActivity;
import hk.m4s.lr.repair.test.utils.MoneyTool;
import hk.m4s.lr.repair.test.utils.ToastUtil;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class GoodsCarAdapter extends BaseAdapter{
    public List<GoodsCarModel.ListBean> list=new ArrayList<>();
    GoodsCarActivity context;
    LayoutInflater inflater;
    int counts;
    public GoodsCarAdapter(GoodsCarActivity context, List<GoodsCarModel.ListBean> data){
        this.context=context;
        this.list=data;
        counts=list.size();
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.list_item_goods_car,parent,false);
            holder.order_shop_size=(TextView)convertView.findViewById(R.id.order_shop_size);
            holder.car_shop_name=(TextView)convertView.findViewById(R.id.car_shop_name);
            holder.order_shop_price=(TextView)convertView.findViewById(R.id.order_shop_price);
            holder.order_shop_num=(TextView)convertView.findViewById(R.id.order_shop_num);
            holder.order_shop_fan=(TextView)convertView.findViewById(R.id.order_shop_fan);
            holder.user_img=(ImageView) convertView.findViewById(R.id.order_shop_img);
            holder.car_shop_select=(ImageView) convertView.findViewById(R.id.car_shop_select);
            holder.car_update=(Button) convertView.findViewById(R.id.car_update);
            holder.car_delete=(Button) convertView.findViewById(R.id.car_delete);
            holder.num_reduce=(Button) convertView.findViewById(R.id.num_reduce);
            holder.num_add=(Button) convertView.findViewById(R.id.num_add);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.car_shop_select.setTag(position);
        holder.num_reduce.setTag(position);
        holder.num_add.setTag(position);
        holder.car_delete.setTag(position);
        holder.car_update.setTag(position);
        GoodsCarModel.ListBean newsModel=list.get(position);
        holder.car_shop_name.setText(newsModel.getGoods_name());
        holder.order_shop_price.setText(MoneyTool.getLocalMoney(newsModel.getGoods_price()));
        holder.order_shop_num.setText(newsModel.getGoods_num()+"");
        holder.order_shop_size.setText(newsModel.getProperty());
        if(newsModel.getSelect().equals("1")){
            holder.car_shop_select.setImageResource(R.mipmap.couponcheckd);
        }else{
            holder.car_shop_select.setImageResource(R.mipmap.couponcheck);
        }
        try {
            Glide.with(context).load(newsModel.getImg()).asBitmap().error(R.mipmap.device_list_no_img).into(holder.user_img);
        }catch (Exception e){
            e.printStackTrace();
        }
        holder.car_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                GoodsCarModel.ListBean newsModel=list.get(num);
                //context.updateGoods(newsModel);

            }
        });
        holder.car_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                GoodsCarModel.ListBean newsModel=list.get(num);
                //context.deleteGoods(newsModel);

            }
        });
        holder.car_shop_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                GoodsCarModel.ListBean newsModel=list.get(num);
                if(newsModel.getSelect().equals("1")){
                    counts--;
                    newsModel.setSelect("2");
                }else{
                    counts++;
                    newsModel.setSelect("1");
                }
                list.set(num,newsModel);
                notifyDataSetChanged();
                context.checkAll(counts,num);
            }
        });
        holder.num_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();

                GoodsCarModel.ListBean newsModel = list.get(num);
                int nums=Integer.parseInt(newsModel.getGoods_num())-1;
                if (nums < 1) {
                    nums = 1;
                    ToastUtil.toast(context, "数量最少为1");
                    newsModel.setGoods_num(nums+"");
                } else{
                    newsModel.setGoods_num(nums+"");
                 }
                list.set(num,newsModel);
                notifyDataSetChanged();
                context.update();
            }
        });
        holder.num_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();
                GoodsCarModel.ListBean newsModel = list.get(num);
                int nums=Integer.parseInt(newsModel.getGoods_num())+1;
                newsModel.setGoods_num(nums+"");
                list.set(num,newsModel);
                notifyDataSetChanged();
                context.update();
            }
        });
        return convertView;
    }

    public static class ViewHolder{
        TextView order_shop_size,car_shop_name,order_shop_price,order_shop_num,order_shop_fan;
        ImageView user_img,car_shop_select;
        Button car_update,car_delete,num_reduce,num_add;
    }
}
